package com.github.postsanf.yinian.fragment;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.LoginActivity;
import com.github.postsanf.yinian.activity.LoginWeChatActivity;
import com.github.postsanf.yinian.utils.SpUtils;


public class GuideFragment4 extends Fragment {
    private static final int WHAT_ANIMATION1 = 5;
    private static final int WHAT_ANIMATION2 = 6;
    private static final int WHAT_ANIMATION3 = 7;
    private static final int WHAT_ANIMATION4 = 8;

    private Context context;
    private ImageView guide4_iv1, guide4_iv2, guide4_iv3;
    private ImageView guide4_content1, guide4_content2, guide4_content3;
    private ImageView tv_register;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case WHAT_ANIMATION1:
                    guide4_iv1.setVisibility(View.VISIBLE);
                    guide4_content1.setVisibility(View.VISIBLE);
                    ObjectAnimator start1 = ObjectAnimator.ofFloat(guide4_content1, "alpha", 0.0f, 1.0f);
                    start1.setDuration(700);
                    start1.setRepeatCount(0);
                    start1.start();
                    ObjectAnimator start11 = ObjectAnimator.ofFloat(guide4_iv1, "alpha", 0.0f, 1.0f);
                    start11.setDuration(700);
                    start11.setRepeatCount(0);
                    ObjectAnimator scaleY = ObjectAnimator.ofFloat(guide4_iv1, "translationY", -37, 0);
                    AnimatorSet animatorSet = new AnimatorSet();
                    animatorSet.playTogether(start11, scaleY);
                    animatorSet.setDuration(700);
                    animatorSet.start();
                    break;
                case WHAT_ANIMATION2:
                    guide4_iv2.setVisibility(View.VISIBLE);
                    guide4_content2.setVisibility(View.VISIBLE);
                    ObjectAnimator start2 = ObjectAnimator.ofFloat(guide4_content2, "alpha", 0.0f, 1.0f);
                    start2.setDuration(640);
                    start2.setRepeatCount(0);
                    start2.start();
                    ObjectAnimator start22 = ObjectAnimator.ofFloat(guide4_iv2, "alpha", 0.0f, 1.0f);
                    start22.setDuration(640);
                    start22.setRepeatCount(0);
                    ObjectAnimator scaleY2 = ObjectAnimator.ofFloat(guide4_iv2, "translationY", 48, 0);
                    AnimatorSet animatorSet2 = new AnimatorSet();
                    animatorSet2.playTogether(start22, scaleY2);
                    animatorSet2.setDuration(640);
                    animatorSet2.start();
                    break;
                case WHAT_ANIMATION3:
                    guide4_iv3.setVisibility(View.VISIBLE);
                    guide4_content3.setVisibility(View.VISIBLE);
                    ObjectAnimator start3 = ObjectAnimator.ofFloat(guide4_content3, "alpha", 0.0f, 1.0f);
                    start3.setDuration(700);
                    start3.setRepeatCount(0);
                    start3.start();
                    ObjectAnimator start33 = ObjectAnimator.ofFloat(guide4_iv3, "alpha", 0.0f, 1.0f);
                    start33.setDuration(700);
                    start33.setRepeatCount(0);
                    ObjectAnimator scaleX3 = ObjectAnimator.ofFloat(guide4_iv3, "translationX", -34, 0);
                    ObjectAnimator scaleY3 = ObjectAnimator.ofFloat(guide4_iv3, "translationY", -34, 0);
                    AnimatorSet animatorSet3 = new AnimatorSet();
                    animatorSet3.playTogether(start33, scaleX3, scaleY3);
                    animatorSet3.setDuration(700);
                    animatorSet3.start();
                    break;
                case WHAT_ANIMATION4:
                    tv_register.setVisibility(View.VISIBLE);
                    ObjectAnimator starts = ObjectAnimator.ofFloat(tv_register, "alpha", 0.0f, 1.0f);
                    starts.setDuration(530);
                    starts.setRepeatCount(0);
                    starts.start();
                    break;
            }
        }
    };

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        registerBoradcastReceiver();
    }

    // 注册接收者
    private void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction("updateGuideShow");
        getActivity().registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case "updateGuideShow":
                    handler.sendEmptyMessageDelayed(WHAT_ANIMATION1, 430);
                    handler.sendEmptyMessageDelayed(WHAT_ANIMATION2, 1130);
                    handler.sendEmptyMessageDelayed(WHAT_ANIMATION3, 2030);
                    handler.sendEmptyMessageDelayed(WHAT_ANIMATION4, 2170);
                    break;
            }
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(mBroadcastReceiver);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        context = getActivity();
        View view = inflater
                .inflate(R.layout.fragment_guide4, container, false);
        guide4_iv1 = (ImageView) view.findViewById(R.id.guide4_iv1);
        guide4_iv2 = (ImageView) view.findViewById(R.id.guide4_iv2);
        guide4_iv3 = (ImageView) view.findViewById(R.id.guide4_iv3);
        guide4_content1 = (ImageView) view.findViewById(R.id.guide4_content1);
        guide4_content2 = (ImageView) view.findViewById(R.id.guide4_content2);
        guide4_content3 = (ImageView) view.findViewById(R.id.guide4_content3);
        tv_register = (ImageView) view.findViewById(R.id.tv_register);
        tv_register.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent(getActivity(), LoginActivity.class));
                startActivity(new Intent(getActivity(), LoginWeChatActivity.class));
                SpUtils.put(getActivity(), "welcomeGuide", true);
                getActivity().finish();
            }
        });
        return view;
    }


}
